package com.mmall.controller.protal;

import com.mmall.commons.Const;
import com.mmall.commons.ServerResponse;
import com.mmall.pojo.User;
import com.mmall.service.IUserService;
import com.mmall.util.CheckUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * 普通用户Controller
 * @author wgt
 * @date 2018/6/23
 * @project mmall-server
 */
@Controller
@RequestMapping("/user")
public class UserController {

    /**
     * 用户信息业务层
     */
    @Autowired
    private IUserService userService;

    /**
     * 用户登录
     * @param username
     * @param password
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/login.do", method = RequestMethod.POST)
    public ServerResponse<User> login(String username, String password, HttpSession session){
        ServerResponse<User> response = userService.login(username,password);

        if (response.isSuccess()){
            session.setAttribute(Const.CURRENT_USER, response.getData());
        }
        return response;
    }

    /**
     * 用户登出
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/logout.do", method = RequestMethod.GET)
    public ServerResponse logout(HttpSession session){
        session.removeAttribute(Const.CURRENT_USER);
        return ServerResponse.createBySuccessMessage("登出成功!");
    }

    /**
     * 用户注册
     * @param user
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/register.do", method = RequestMethod.POST)
    public ServerResponse register(User user){
        return userService.register(user);
    }

    /**
     * 检查用户名和email
     * @param str
     * @param type
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/check_valid.do", method = RequestMethod.POST)
    public ServerResponse checkValid(String str, String type){
        return userService.checkValid(str, type);
    }

    /**
     * 获取当前用户信息
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/get_user_info.do", method = RequestMethod.GET)
    public ServerResponse getUserInfo(HttpSession session){
        ServerResponse<User> serverResponse = CheckUserUtil.checkLogin(session);
        if (serverResponse.isSuccess()){
            return ServerResponse.createBySuccess(serverResponse.getData());
        }
        return serverResponse;
    }

    /**
     * 忘记密码，获取用户问题信息
     * @param username
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/forget_get_question.do", method = RequestMethod.GET)
    public ServerResponse selectQuestion(String username){
        return userService.selectQuestion(username);
    }

    /**
     * 检查问题答案
     * @param username
     * @param question
     * @param answer
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/forget_check_answer.do", method = RequestMethod.POST)
    public ServerResponse checkAnswer(String username, String question, String answer){
        return userService.checkAnswer(username, question, answer);
    }

    /**
     * 忘记密码情况 使用token重置密码
     * @param username
     * @param password
     * @param token
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/forget_reset_password.do", method = RequestMethod.POST)
    public ServerResponse forgetResetPassword(String username, String password, String token){
        return userService.forgetResetPassword(username, password, token);
    }

    /**
     * 登陆状态重置密码
     * @param oldPassword
     * @param newPassword
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/reset_password.do", method = RequestMethod.POST)
    public ServerResponse resetPassword(String oldPassword, String newPassword, HttpSession session){
        ServerResponse<User> serverResponse = CheckUserUtil.checkLogin(session);
        if (serverResponse.isSuccess()){
            User user = serverResponse.getData();
            return userService.resetPassword(user.getUsername(), oldPassword, newPassword);
        }
        return serverResponse;
    }

    /**
     * 更新用户信息
     * @param user
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/update_information.do", method = RequestMethod.POST)
    public ServerResponse updateInformation(User user, HttpSession session){
        ServerResponse<User> checkResponse = CheckUserUtil.checkLogin(session);
        if (checkResponse.isSuccess()) {
            User sessionUser = checkResponse.getData();
            user.setId(sessionUser.getId());
            ServerResponse serverResponse = userService.updateInformation(user);
            if (serverResponse.isSuccess()) {
                session.setAttribute(Const.CURRENT_USER, serverResponse.getData());
            }
        }
        return checkResponse;
    }

    /**
     * 获取当前用户信息
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/get_information.do", method = RequestMethod.GET)
    public ServerResponse getUserInformation(HttpSession session){
        ServerResponse<User> checkResponse = CheckUserUtil.checkNeedLogin(session);
        if (checkResponse.isSuccess()){
            User user = checkResponse.getData();
            return userService.getUserInformation(user.getId());
        }
        return checkResponse;
    }

}
